import 'package:flutter/material.dart';
import 'package:flutter_demo/provider/active.dart';
import 'package:flutter_demo/services/api.dart';
import 'package:flutter_demo/utils/screen_size_fit.dart';
import 'package:provider/provider.dart';
import 'package:toast/toast.dart';

class DetailPage extends StatefulWidget {
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  void initState(){
    super.initState();
  }
  get index => Provider.of<HomeProvider>(context,listen: false).currIndex;
  get book => Provider.of<HomeProvider>(context).books[index];
  final _formKey = GlobalKey<FormState>();
  TextEditingController nameController = new TextEditingController();
  TextEditingController phoneController = new TextEditingController();
  TextEditingController addressController = new TextEditingController();

  OutlineInputBorder inputBorder = OutlineInputBorder(
      borderSide: BorderSide(
          // color: Color.fromRGBO(207, 207, 207, .2),
          color: Color.fromRGBO(255, 255, 255, 1),
          width: 1.w,
          style: BorderStyle.solid),
      borderRadius: BorderRadius.circular(14.w));

  Widget bookInfo() {
    return Container(
      width: 750.w,
      padding: EdgeInsets.only(top: 50.w),
      decoration: BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomLeft,
              colors: [
            Color.fromRGBO(244, 181, 125, 1),
            Color.fromRGBO(235, 74, 48, 1)
          ])),
      child: Column(
        children: [
          Image.network(book['pic'], width: 506.w, height: 554.w),
          SizedBox(
            height: 20.w,
          ),
          Text(book['name'],
              textAlign: TextAlign.center,
              style: new TextStyle(
                color: Color.fromRGBO(255, 255, 255, 1),
                fontSize: 57.sp,
                fontWeight: FontWeight.bold,
                decoration: TextDecoration.none,
              )),
          SizedBox(
            height: 20.w,
          ),
        ],
      ),
    );
  }

  Widget takeBookForm() {
    return Padding(
      padding: EdgeInsets.all(30.w),
      child: Column(
        children: [
          Form(
              key: _formKey,
              child: Column(
                children: [
                  TextFormField(
                    controller: nameController,
                    decoration: InputDecoration(
                        hintText: '请填写收货人姓名',
                        contentPadding: EdgeInsets.symmetric(
                            horizontal: 35.w, vertical: 25.w),
                        border: inputBorder),
                    validator: (value) {
                      if (value.isEmpty) {
                        return '请输入用户名';
                      }
                      return null;
                    },
                  ),
                  SizedBox(height: 20.w),
                  TextFormField(
                    controller: phoneController,
                    keyboardType: TextInputType.number,
                    decoration: InputDecoration(
                        hintText: '请填写收货人手机号码',
                        contentPadding: EdgeInsets.symmetric(
                            horizontal: 35.w, vertical: 25.w),
                        border: inputBorder),
                    validator: (value) {
                      RegExp reg = new RegExp(r'^\d{11}$');
                      if (!reg.hasMatch(value)) {
                        return '请输入11位手机号码';
                      }
                      return null;
                    },
                  ),
                  SizedBox(height: 20.w),
                  TextFormField(
                    controller: addressController,
                    maxLines: 3,
                    decoration: InputDecoration(
                        hintText: '请填写收货人详细收货地址(如：省、市、县、街道、小区、单元、门牌号等)',
                        border: inputBorder),
                  )
                ],
              )),
          SizedBox(
            height: 30.w,
          ),
          RaisedButton(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(45.w)),
                  ),
                  padding: EdgeInsets.all(0.0),
                  clipBehavior: Clip.hardEdge,
                  textColor: Color.fromRGBO(255, 255, 255, 1),
                  child: Container(
                      width: 500.w,
                      height: 90.w,
                      decoration: const BoxDecoration(
                        gradient: LinearGradient(
                          colors: <Color>[
                            Color.fromRGBO(255, 117, 97, 1),
                            Color.fromRGBO(255, 45, 7, 1),
                          ],
                        ),
                      ),
                      child: Center(
                        child: Text('提交',
                            textAlign: TextAlign.center,
                            style: new TextStyle(fontSize: 50.w)),
                      )),
                  onPressed: () async {
                    if (_formKey.currentState.validate()) {
                      Map<String, dynamic> data = {
                        'id': index,
                        'name': nameController.text,
                        'phone': phoneController.text,
                        'address': addressController.text,
                      };
                      await Home.postAddress(data);
                      Toast.show('快递将在15天内邮出！',context);
                    }
                  },
                ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenSizeFit(context);
    return Scaffold(
      body: ListView(
        children: [bookInfo(), takeBookForm()],
      ),
    );
  }
}
